import time
import allure
from selenium.webdriver.common.by import By

from cloud_po.pages.base_page import BasePage
from cloud_po.utils.log_utils import logger


class ChannelPage(BasePage):
    __ADD_CHANNEL_BTN = (By.XPATH, "//span[text()='添加渠道']")
    __I_KNOW = (By.CSS_SELECTOR, ".el-message-box__btns span")
    __CHANNEL_NAME =  (By.CSS_SELECTOR,".el-table__body-wrapper.is-scrolling-left .el-table__row>td:nth-child(2)>.cell")
    __INPUT_SEARCH_NAME = (By.XPATH,"(//*[@class='el-input__inner'])[1]")
    __SEARCH_BTN =  (By.XPATH,"//*[@class='el-icon-search']/..")
    __DELETE_CONFIRM = (By.CSS_SELECTOR,".el-button.el-button--default.el-button--small.el-button--primary")


    def click_add_channel(self):
        logger.info("点击添加渠道按钮")
        with allure.step("点击添加渠道按钮"):
            self.do_find(self.__ADD_CHANNEL_BTN).click()
            time.sleep(1)
        from cloud_po.pages.channel_add_page import ChannelAddPage
        return ChannelAddPage(self.driver)

    def get_channel_names(self):
        logger.info("获取所有渠道名称")
        return self.get_names(self.__CHANNEL_NAME)

    def close_alert(self):
        logger.info("关闭弹窗")
        self.web_locate_click(self.__I_KNOW)
        self.safe_click(self.__I_KNOW)

    def search_channel(self,name):
        logger.info("根据渠道名称搜索渠道")
        time.sleep(1)
        with allure.step("渠道名称输入框输入搜索内容"):
            self.do_send_keys(name, self.__INPUT_SEARCH_NAME)
        with allure.step("点击查询"):
            search_btn = self.do_find(self.__SEARCH_BTN)
            # 查询按钮被遮挡，让其显示在页面
            self.scroll_to_element(self.__SEARCH_BTN)
            search_btn.click()
        return self.get_channel_names()

    def clean_data(self,name):
        logger.info("清理测试数据")
        with allure.step("清理渠道测试数据"):
            self.safe_click((By.XPATH, f"(//*[text()='{name}']/../..//span[text()='删除'])[2]"))
        return ChannelPage(self.driver)

    def close_delete_alert(self):
        logger.info("关闭确认删除弹窗")
        self.web_locate_click(self.__DELETE_CONFIRM)
        self.safe_click(self.__DELETE_CONFIRM)